package org.game.porker.porker24.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


import org.apache.log4j.Logger;

public class JdbcPropertyReader {
	private static Logger logger = Logger.getLogger(JdbcPropertyReader.class);
	private static String filename = "/jdbc.properties";
	private static String usernameKey = "username";
	private static String password = "password";
	private static Properties prop = null;//new Properties();

	public JdbcPropertyReader() {
		// TODO Auto-generated constructor stub
	}
	
	public static String getDbUsername() {
		if(prop == null) {
			initProp();
		}
		if (prop == null) return "";
		return prop.getProperty(usernameKey, "");
	}
	
	public static String getDbPassword() {
		if(prop == null) {
			initProp();
		}
		if (prop == null) return "";
		return prop.getProperty(password, "");
	}
	
	private synchronized static void initProp() {
		if (prop == null) {
			InputStream is = JdbcPropertyReader.class.getResourceAsStream(filename);
			Properties p = new Properties();
			try {
				p.load(is);
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
//				e.printStackTrace();
				logger.error(e.getMessage(), e);
				return;
			} finally {
				try {
					if(is != null) is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
//					e.printStackTrace();
				}
			}
			prop = p;
		}
	}

}
